#include "threadPool.h"
#define MAX_EVENTS 2
#define BUFFER_SIZE 4096

int exitPipe[2];
void sigFunc(int signum)
{
    printf("signum = %d\n", signum);
    write(exitPipe[1], "1", 1);
    printf("parent got signal\n");
}
int main(int argc, char *argv[])
{
    // ./client 192.168.253.128 1234
    ARGS_CHECK(argc, 3);
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(atoi(argv[2]));
    serverAddr.sin_addr.s_addr = inet_addr(argv[1]);

    // argv[1] "192.168.118.128" --> 32bit的大端ip地址
    usraddr usr;
    int ret = connect(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
    ERROR_CHECK(ret, -1, "connect");
    // 循环输入用户名和密码，直到输入正确
    // 输入用户名，密码
    char encryptedStr[50] = {0};
    while (1)
    {
        int regFlag = 0;
        printf("wheather you need to register: 1 for yes,0 for no:");
        scanf("%d", &regFlag);
        send(sockfd, &regFlag, sizeof(int), 0);
        if (regFlag == 0)
        {
            memset(&usr, 0, sizeof(usr));
            printf("Please input usrAddress :");
            scanf("%s", usr.usrAddr);
            printf("Enter password :");
            scanf("%s", usr.pwd);
            // 发送用户名
            char key[100];
            strcpy(key, usr.pwd);
            usr.dataLen = strlen(usr.usrAddr);
            send(sockfd, &usr.dataLen, sizeof(int), 0);
            sendn(sockfd, usr.usrAddr, usr.dataLen);
            // 接受盐值
            recv(sockfd, &usr.dataLen, sizeof(int), 0);
            recvn(sockfd, usr.pwd, usr.dataLen);
            // 转换成密文
            strcpy(encryptedStr, crypt(key, usr.pwd));
            // 发送密文
            strcpy(usr.pwd, encryptedStr);
            usr.dataLen = strlen(usr.pwd);
            send(sockfd, &usr.dataLen, sizeof(int), 0);
            sendn(sockfd, usr.pwd, usr.dataLen);
            // 接收验证结果，根据验证结果来决定是否要继续登录
            // 根据结果来判断是注册还是登录
            recv(sockfd, &usr.flag, sizeof(int), 0);
            if (usr.flag == 0) // 密码正确
            {
                break;
            }
        }
        else if (regFlag == 1)
        {
            memset(&usr, 0, sizeof(usr));
            printf("Please input new usrAddress :");
            scanf("%s", usr.usrAddr);
            printf("Enter new password :");
            scanf("%s", usr.pwd);
            // 发送用户名
            usr.dataLen = strlen(usr.usrAddr);
            send(sockfd, &usr.dataLen, sizeof(int), 0);
            sendn(sockfd, usr.usrAddr, usr.dataLen);
            // 发送密码
            usr.dataLen = strlen(usr.pwd);
            send(sockfd, &usr.dataLen, sizeof(int), 0);
            sendn(sockfd, usr.pwd, usr.dataLen);
            int regDone = 0;
            recv(sockfd, &regDone, sizeof(int), 0);
            if (1==regDone )
            {
                printf("register succcess\n");
            }
            else{
                printf("Usr is aready exist,Use diffrent name\n");
            }
        }
    }
    puts("pwd correct\n");

    // 启动epoll 监听sockfd
    int epfd = epollCtor();
    char buf[BUFFER_SIZE];
    char recvBuf[BUF_SIZE], sendBuf[BUF_SIZE];
    int recvLen;
    fileTrain file;
    while (1)
    {
        while (bzero(buf, sizeof(buf)), read(STDIN_FILENO, buf, sizeof(buf)) > 0)
        {
            int count, i;
            char **words = split_words(buf, &count);
            int sendLen = strlen(buf);
            send(sockfd, &sendLen, sizeof(int), 0); // 发命令给服务器端
            sendn(sockfd, buf, sendLen);

            if (2 == count)
            {
                if (strcmp(words[0], "gets") == 0)
                {
                    system("clear");
                    printf("start recvFile\n");
                    recvFile(sockfd);
                    printf("recvFile finished\n");
                }
                else if (strcmp(words[0], "puts") == 0)
                {
                    system("clear");
                    memset(file.MD5,0,sizeof(file.MD5));
                    md5(words[1],file.MD5);
                    //发送MD5码
                    
                    
                    char filename[33];
                    int i;
                    for (i = 0; i < 16; i++)
                    {
                        sprintf(filename + i * 2, "%02x", file.MD5[i]);
                    }
                    filename[32] = '\0';
                    strcpy(file.MD5,filename);
                    file.dataLen= strlen(file.MD5);
                    send(sockfd, &file.dataLen, sizeof(int), 0);
                    sendn(sockfd, file.MD5, file.dataLen);

                    int fExist;
                    recv(sockfd, &fExist, sizeof(int), 0);
                    
                    if(0==fExist)
                    {//文件不存在
                        //发送文件
                        printf("%d\n", fExist);
                        printf("start transFile\n");
                        transFile(sockfd, words[1]);
                        printf("transFile finished\n");
                    }else{
                        printf("极速秒传\n");
                    }
                    
                   
                }
                else if (strcmp(words[0], "cd") == 0)
                {
                    system("clear");
                    int cFlag;
                    recv(sockfd, &cFlag, sizeof(int), 0); // 接cd之后的路径
                    if(1==cFlag){
                        memset(recvBuf,0,sizeof(recvBuf));
                        recv(sockfd, &recvLen, sizeof(int), 0); // 接cd之后的路径
                        recvn(sockfd, recvBuf, recvLen);
                        printf("%s\n", recvBuf);
                    }else{
                        printf("wrong cd dir commond\n");
                    }
                   
                }
                else if (strcmp(words[0], "ls") == 0)
                {
                    system("clear");
                    {
                        recv(sockfd, &recvLen, sizeof(int), 0); // 接文件信息
                        if (recvLen == 0)
                        {
                            break;
                        }
                        recvn(sockfd, recvBuf, recvLen);
                        printf("%s\n", recvBuf);
                    }
                }
                else if (strcmp(words[0], "remove") == 0)
                {
                    system("clear");
                    recv(sockfd, &recvLen, sizeof(int), 0); // 接cd之后的路径
                    recvn(sockfd, recvBuf, recvLen);
                    printf("%s\n", recvBuf);
                }
                else
                {
                    printf("Wrong Commond\n");
                }
            }
            else if (1 == count)
            {
                if (strcmp(words[0], "pwd") == 0)
                {
                    system("clear");
                    recv(sockfd, &recvLen, sizeof(int), 0); // 接cd之后的路径
                    recvn(sockfd, recvBuf, recvLen);
                    printf("%s\n", recvBuf);
                }
                else
                {
                    printf("Wrong Commond\n");
                }
            }
            else
            {
                printf("Wrong Commond\n");
            }
        }
    }
}